home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / CADSP 1.0 / Messenger Classes / CADSP.c next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  8.1 KB  |  418 lines  |  [TEXT/KAHL]

  1. /***
  2.  * CADSP
  3.  *
  4.  *        AppleTalk Data Stream Protocol handler
  5.  *
  6.  *        this object handles data stream connections over appletalk
  7.  *
  8.  *        Copyright © 1992 Bernard Bernstein. All rights reserved.
  9.  ***/ 
  10.  
  11.  
  12. #include <Global.h>
  13.  
  14. #include "CADSP.h"
  15.  
  16.  
  17. /***
  18.  * IADSP
  19.  *
  20.  *        Initialize the ADSP handler.
  21.  *
  22.  *            queueSize    -    size of the internal in/out queues
  23.  *            dataSize    -    size of the external queues
  24.  *
  25.  ***/
  26. void CADSP::IADSP(short queueSize, short dataSize)
  27. {
  28.     short mppRefNum;
  29.     short dspRefNum;
  30.     Ptr sendQ = nil;
  31.     Ptr recvQ = nil;
  32.     Ptr dspattnBuf = nil;
  33.     Ptr inBuf = nil;
  34.     Ptr outBuf = nil;
  35.     Ptr attnBuf = nil;
  36.     DSPPBPtr dsppb = nil;
  37.     TPCCB ccb = nil;
  38.     
  39.     initialized = false;
  40.     opened = false;
  41.  
  42.     FailOSErr(OpenDriver("\p.MPP", &mppRefNum));
  43.     FailOSErr(OpenDriver("\p.DSP", &dspRefNum));
  44.  
  45.     itsDspRefNum = dspRefNum;
  46.     itsQueueSize = queueSize;
  47.  
  48.     TRY
  49.         {
  50.         // make non-relocatable blocks for ADSP internal use
  51.         sendQ = NewPtr(queueSize);
  52.         FailNIL(sendQ);
  53.         recvQ = NewPtr(queueSize);
  54.         FailNIL(recvQ);
  55.         dspattnBuf = NewPtr(attnBufSize);
  56.         FailNIL(dspattnBuf);
  57.  
  58.         ccb = (TPCCB)NewPtr(sizeof(TRCCB));
  59.         FailNIL(ccb);
  60.         
  61.  
  62.         // make non-relocatable blocks for my buffers
  63.         inBuf = NewPtr(dataSize);
  64.         FailNIL(inBuf);
  65.         outBuf = NewPtr(dataSize);
  66.         FailNIL(outBuf);
  67.         attnBuf = NewPtr(dataSize);
  68.         FailNIL(attnBuf);
  69.  
  70.  
  71.         // make my parameter block non-relocatable        
  72.         dsppb = (DSPPBPtr)NewPtr(sizeof(DSPParamBlock));
  73.         FailNIL(dsppb);
  74.         
  75.         
  76.         // remember all that stuff so we can deallocate it when disposed
  77.         dspRecvQ = recvQ;
  78.         dspSendQ = sendQ;
  79.         dspAttnBuf = dspattnBuf;
  80.         itsCcb = ccb;
  81.  
  82.         itsInBuf = inBuf;
  83.         itsOutBuf = outBuf;
  84.         itsAttnBuf = attnBuf;
  85.  
  86.         itsDsppb = dsppb;
  87.         }
  88.     CATCH
  89.         {
  90.         ForgetPtr(sendQ);
  91.         ForgetPtr(recvQ);
  92.         ForgetPtr(dspattnBuf);
  93.         ForgetPtr(inBuf);
  94.         ForgetPtr(outBuf);
  95.         ForgetPtr(attnBuf);
  96.         ForgetPtr(dsppb);
  97.         ForgetPtr(ccb);
  98.         }
  99.     ENDTRY;
  100. }
  101.  
  102.  
  103. /***
  104.  * Dispose
  105.  *
  106.  *        Kill pointers for ATP
  107.  ***/
  108. void CADSP::Dispose(void)
  109. {
  110.     DSPRemove();
  111.  
  112.     ForgetPtr(dspRecvQ);
  113.     ForgetPtr(dspSendQ);
  114.     ForgetPtr(dspAttnBuf);
  115.     ForgetPtr(itsInBuf);
  116.     ForgetPtr(itsOutBuf);
  117.     ForgetPtr(itsAttnBuf);
  118.     ForgetPtr(itsCcb);
  119.     ForgetPtr(itsDsppb);
  120.     
  121.     inherited::Dispose();
  122. }
  123.  
  124.  
  125.  
  126.  
  127. /**********************************************************************
  128.  
  129.                 Accessing connection information
  130.             
  131. **********************************************************************/
  132.  
  133.  
  134. /***
  135.  * DSPioResult
  136.  *
  137.  *        The current ioResult. Needed when waiting for asynchronous
  138.  *        calls to complete.
  139.  ***/
  140. short CADSP::DSPioResult(void)
  141. {
  142.     return itsDsppb->ioResult;
  143. }
  144.  
  145.  
  146.  
  147. /**********************************************************************
  148.  
  149.             Establishing and Terminating an ADSP connection
  150.             
  151. **********************************************************************/
  152.  
  153.  
  154. /***
  155.  * DSPInit
  156.  *
  157.  *        Setup the ADSPConnector
  158.  ***/
  159. void CADSP::DSPInit(short *socket)
  160. {
  161.     // setup dspInit parameters
  162.     itsDsppb->ioNamePtr = nil;
  163.     itsDsppb->ioCRefNum = itsDspRefNum;
  164.     itsDsppb->ioCompletion = nil;
  165.     itsDsppb->ccbRefNum = 0;
  166.     itsDsppb->csCode = dspInit;
  167.     
  168.     itsDsppb->u.initParams.ccbPtr = itsCcb;
  169.     itsDsppb->u.initParams.userRoutine = nil;
  170.     itsDsppb->u.initParams.sendQSize = itsQueueSize;
  171.     itsDsppb->u.initParams.recvQSize = itsQueueSize;
  172.     itsDsppb->u.initParams.sendQueue = (uPtr)dspSendQ;
  173.     itsDsppb->u.initParams.recvQueue = (uPtr)dspRecvQ;
  174.     itsDsppb->u.initParams.attnPtr = (uPtr)dspAttnBuf;
  175.     itsDsppb->u.initParams.localSocket = *socket;
  176.     
  177.     itsCcb->refNum = 0;
  178.     
  179.     // make this connection
  180.     FailOSErr(PBControl(itsDsppb, false));
  181.     
  182.     // remember the socket and refnum
  183.     *socket = itsDsppb->u.initParams.localSocket;
  184.     itsCcb->refNum = itsDsppb->ccbRefNum;
  185.     
  186.     initialized = true;
  187. }
  188.  
  189.  
  190.  
  191.  
  192. /***
  193.  * DSPOptions
  194.  *
  195.  *        Set the options for the conneciton
  196.  *
  197.  *            zero for sendBlocking or badSeqMax will retain
  198.  *            their old values.
  199.  *
  200.  ***/
  201. void CADSP::DSPOptions(short sendBlocking, short badSeqMax, char useCheckSum)
  202. {
  203.     ASSERT(initialized);
  204.  
  205.     // set up dspOptions parameters
  206.     itsDsppb->ioCRefNum = itsDspRefNum;
  207.     itsDsppb->csCode = dspOptions;
  208.     itsDsppb->ccbRefNum = itsCcb->refNum;
  209.     
  210.     itsDsppb->u.optionParams.sendBlocking = sendBlocking;
  211.     itsDsppb->u.optionParams.badSeqMax = badSeqMax;
  212.     itsDsppb->u.optionParams.useCheckSum = useCheckSum;
  213.  
  214.     // set the options
  215.     FailOSErr(PBControl(itsDsppb, false));
  216. }
  217.  
  218.  
  219.  
  220.  
  221. /***
  222.  * DSPOpen
  223.  *
  224.  *        Open a connection
  225.  *
  226.  *            ocMode takes
  227.  *                ocPassive    -    for passive open
  228.  *                ocRequest    -    for active open
  229.  *
  230.  *            
  231.  *            for Active open:
  232.  *                    'addr' contains the remote address
  233.  *
  234.  *            for Passive open:
  235.  *                    'addr' contains the filter address
  236.  *                    the call is made asynchronously
  237.  *                    you can poll the ioResult until
  238.  *                    a connection is made.
  239.  *
  240.  *            There are two other open types, ocAccept and ocEstablish.
  241.  *            ••• they could be implemented in the future
  242.  ***/
  243. void CADSP::DSPOpen(AddrBlock addr, unsigned char ocMode)
  244. {
  245.     OSErr err;
  246.     Boolean asynchronous = (ocMode == ocPassive);
  247.     
  248.     ASSERT(initialized);
  249.     
  250.     if (opened)
  251.         {
  252.         DSPClose();
  253.         }
  254.  
  255.     // set up dspOpen parameters
  256.     itsDsppb->ioCRefNum = itsDspRefNum;
  257.     itsDsppb->csCode = dspOpen;
  258.     itsDsppb->ccbRefNum = itsCcb->refNum;
  259.     
  260.     itsDsppb->u.openParams.remoteAddress = addr;
  261.     itsDsppb->u.openParams.filterAddress = addr;
  262.     itsDsppb->u.openParams.ocMode = ocMode;
  263.     itsDsppb->u.openParams.ocInterval = 0;
  264.     itsDsppb->u.openParams.ocMaximum = 0;
  265.     
  266.     // open the connection
  267.     FailOSErr(PBControl(itsDsppb, asynchronous));
  268.  
  269.     opened = true;
  270. }
  271.  
  272.  
  273.  
  274. /***
  275.  * DSPClose
  276.  *
  277.  *        Close the connection
  278.  ***/
  279. void CADSP::DSPClose(void)
  280. {
  281.     if (opened)
  282.         {
  283.         // set up dspClose parameters
  284.         itsDsppb->ioCRefNum = itsDspRefNum;
  285.         itsDsppb->csCode = dspClose;
  286.         itsDsppb->ccbRefNum = itsCcb->refNum;
  287.         itsDsppb->u.closeParams.abort = true;
  288.         
  289.         // do the close
  290.         FailOSErr(PBControl(itsDsppb, false));
  291.         
  292.         opened = false;
  293.         }
  294. }
  295.  
  296.  
  297.  
  298.  
  299. /***
  300.  * DSPRemove
  301.  *
  302.  *        Remove the connection completely
  303.  *        After this, the memory can be deallocated
  304.  ***/
  305. void CADSP::DSPRemove(void)
  306. {
  307.     if (opened)
  308.         {
  309.         // set up dspClose parameters
  310.         itsDsppb->ioCRefNum = itsDspRefNum;
  311.         itsDsppb->csCode = dspRemove;
  312.         itsDsppb->ccbRefNum = itsCcb->refNum;
  313.         itsDsppb->u.closeParams.abort = true;
  314.         
  315.         FailOSErr(PBControl(itsDsppb, false));
  316.         
  317.         opened = false;
  318.         initialized = false;
  319.         }
  320. }
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. /**********************************************************************
  328.  
  329.                     Maintaining an ADSP connection
  330.             
  331. **********************************************************************/
  332.  
  333.  
  334. /***
  335.  * DSPStatus
  336.  *
  337.  *        Get status information for the connection
  338.  *
  339.  *            sendPending    -    bytes in the send queue not sent yet
  340.  *            sendAvail    -    amount of available space in send queue
  341.  *            recvPending    -    bytes in the receive queue not read yet
  342.  *            recvAvail    -    amount of available space in receive queue
  343.  *
  344.  ***/
  345. void CADSP::DSPStatus(short *sendPending, short *sendAvail, 
  346.                         short *recvPending, short *recvAvail)
  347. {
  348.     ASSERT(opened);
  349.  
  350.     // setup the dspStatus parameters
  351.     itsDsppb->ioCRefNum = itsDspRefNum;
  352.     itsDsppb->csCode = dspStatus;
  353.     itsDsppb->ccbRefNum = itsCcb->refNum;
  354.     
  355.     // get the status
  356.     FailOSErr(PBControl(itsDsppb, false));
  357.     
  358.     *sendPending = itsDsppb->u.statusParams.sendQPending;
  359.     *sendAvail = itsDsppb->u.statusParams.sendQFree;
  360.     *recvPending = itsDsppb->u.statusParams.recvQPending;
  361.     *recvAvail = itsDsppb->u.statusParams.recvQFree;
  362. }
  363.  
  364.  
  365.  
  366.  
  367.  
  368. /***
  369.  * DSPRead
  370.  *
  371.  *        Read some data from the connection
  372.  ***/
  373. void CADSP::DSPRead(void *buffer, short amountToRead, short *amountRead)
  374. {
  375.     ASSERT(opened);
  376.  
  377.     // set up dspRead parameters
  378.     itsDsppb->ioCRefNum = itsDspRefNum;
  379.     itsDsppb->csCode = dspRead;
  380.     itsDsppb->ccbRefNum = itsCcb->refNum;
  381.     
  382.     itsDsppb->u.ioParams.reqCount = amountToRead;
  383.     itsDsppb->u.ioParams.dataPtr = (uPtr)buffer;
  384.     
  385.     // read from the connection
  386.     FailOSErr(PBControl(itsDsppb, false));
  387.     
  388.     *amountRead = itsDsppb->u.ioParams.actCount;
  389. }
  390.  
  391.  
  392.  
  393.  
  394.  
  395. /***
  396.  * DSPWrite
  397.  *
  398.  *        Write some stuff to the connection
  399.  *
  400.  ***/
  401. void CADSP::DSPWrite(void *buffer, short amountToWrite)
  402. {
  403.     ASSERT(opened);
  404.  
  405.     // set up dspWrite parameters
  406.     itsDsppb->ioCRefNum = itsDspRefNum;
  407.     itsDsppb->csCode = dspWrite;
  408.     itsDsppb->ccbRefNum = itsCcb->refNum;
  409.     
  410.     itsDsppb->u.ioParams.reqCount = amountToWrite;
  411.     itsDsppb->u.ioParams.dataPtr = (uPtr)buffer;
  412.     itsDsppb->u.ioParams.eom = 1;
  413.     itsDsppb->u.ioParams.flush = 1;
  414.     
  415.     // write to the connection
  416.     FailOSErr(PBControl(itsDsppb, false));
  417. }
  418.